package com.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.ex.ShoppingException;
import com.shop.feignClient.GoodsClient;
import com.shop.po.R;
import com.shop.user.Interceptor.GetUserInfoInterceptor;
import com.shop.user.mapper.CartMapper;
import com.shop.user.po.Cart;
import com.shop.user.po.User;
import com.shop.user.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author banxia
 * @since 2023-02-25
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {


    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsClient goodsClient;

    @Override
    public void add(Long id,String imagePath) {
        User user = GetUserInfoInterceptor.getUser();
        if (user == null) {
            ShoppingException.ThrowShoppingException("请先登录！");
        }
        //先查询该用户购物车是否存在该商品
        LambdaQueryWrapper<Cart> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Cart::getGoodsBaseId,id);
        wrapper.eq(Cart::getUserId,user.getId());
        Cart one = cartMapper.selectOne(wrapper);
        if (one!=null){
            //存在，数量加1
            addCart(one.getId(),1);
            return;
        }
        //不存在-->直接添加到购物车
        R r = goodsClient.getByGoodsIdFromRedis(id);
        if (r.getCode()==400){
            ShoppingException.ThrowShoppingException("商品数据异常！");
        }
        Map<String, Object> data = r.getData();
        LinkedHashMap<String,Object> map = (LinkedHashMap) data.get("goodsInfo");
        Cart cart = new Cart();
        cart.setUserId(user.getId());
        cart.setGoodsBaseId(id);
//        cart.setGoodsBaseName(goodsInfo.getName());
        cart.setGoodsBaseName(map.get("name").toString());
        cart.setGoodsImagePath(imagePath);
        cart.setCount(1);
        cart.setPriceIdOldPrice(Long.parseLong(map.get("oldPrice").toString()));
        cart.setMerchantId(Long.parseLong(map.get("merchantId").toString()));//商家ID
        cart.setMerchantName(map.get("merchantName").toString());
        int i = cartMapper.insert(cart);
        if (i == 0) {
            ShoppingException.ThrowShoppingException("加入购物车失败！");
        }
    }

    @Override
    public void addCart(Long id, int count) {
        User user = GetUserInfoInterceptor.getUser();
        if (user == null) {
            ShoppingException.ThrowShoppingException("请先登录！");
        }
        int i=cartMapper.updateCount(id,count);
        if (i == 0) {
            ShoppingException.ThrowShoppingException("添加数量失败，请重试");
        }
    }

    @Override
    public List<Cart> getCart() {
        User user = GetUserInfoInterceptor.getUser();
        if (user == null) {
            ShoppingException.ThrowShoppingException("请先登录");
        }
        LambdaQueryWrapper<Cart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId,user.getId());
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        return carts;
    }
}
